import BaseEntity from '@/framework/domain/BaseEntity'
import Id from '@/framework/infra/generator/Id'

const id = Id('skuEntity')
export default class SkuEntity extends BaseEntity {
  constructor(props) {
    super(props)
    this.values = []
    this.stock = this.stock || 0
    this.deltaStock = ''
    this.finalStock = this.stock // 修改的时候使用
    this.isAddStock = true
    this.price = this.price || ''
    this.previewImg = this.previewImg || ''
    this.skuNo = this.skuNo || ''
    this._key = id()
  }

  setSalePropValue(salePropValue) {
    this.values.push(salePropValue)
    if (salePropValue.uploadPicFlag && !this.previewImg) {
      this.previewImg = salePropValue.previewImg
    }
  }

  clearSalePropValues() {
    this.values = []
  }

  isSaleProValuesEq(orderValues) {
    return this.valueIdsIn(orderValues.map(el => el.id))
  }

  valueIdsIn(idList) {
    return !this.values.some(el => !idList.includes(el.id))
  }

  isMatchedOptions(propValueMap) {
    return !this.values.some(el => propValueMap[el.propertyId] !== '0' && propValueMap[el.propertyId] !== el.id)
  }

  setMatchedFieldValue(propValueMap, field, fieldValue) {
    if (!this.isMatchedOptions(propValueMap)) {
      return
    }
    this[field] = fieldValue
  }

  getRealFinalStock() {
    return this.isAddStock ? parseInt(this.stock) + parseInt(this.deltaStock) : parseInt(this.stock) - parseInt(this.deltaStock)
  }

  getRealDeltaStock() {
    return this.isAddStock ? parseInt(this.finalStock) - parseInt(this.stock) : parseInt(this.stock) - parseInt(this.finalStock)
  }

  calcFinalStock() {
    if (!this.deltaStock) {
      this.finalStock = this.stock
    } else {
      const finalStock = this.getRealFinalStock()
      if (finalStock >= 0) {
        this.finalStock = finalStock
      }
    }
  }

  calcDeltaStock() {
    if (!this.finalStock) {
      this.deltaStock = this.stock
    } else {
      const deltaStock = this.getRealDeltaStock()
      if (deltaStock >= 0) {
        this.deltaStock = deltaStock
      } else {
        this.deltaStock = -deltaStock
        this.isAddStock = !this.isAddStock
      }
    }
  }

  getSkuRow() {
    return {
      price: this.price,
      skuNo: this.skuNo,
      previewImg: this.previewImg,
      deltaStock: this.deltaStock,
      isAddStock: this.isAddStock,
      stock: this.stock,
      propertyValueList: this.values.map(el => {
        return {
          propertyValue: el.finalValue,
          propertyId: el.propertyId,
          notes: el.notes,
          propertyValueId: el.id,
          previewImg: el.previewImg,
          valueSource: el.valueSource || 0,
          groupId: el.groupId
        }
      })
    }
  }

  validate(rowNum) {
    if (!this.price || this.price <= 0) {
      throw new Error('第' + rowNum + '行规格的价格必须大于0')
    }
    if (this.values.length > 0 && !this.previewImg) {
      throw new Error('请上传第' + rowNum + '行预览图')
    }
    if (this.getRealFinalStock() < 0) {
      throw new Error('第' + rowNum + '行规格的库存不能少于0')
    }
  }
}
